/*
	Copyright (c) 2012 by karmaleaving@qq.com. All rights RESERVED.
*/
/*
	This file is part of "memory.h" and it defines "class auto_ptr".
	The "auto_ptr" is in standard lib.
*/
#include "utility.h"
#define _KL_USE_AUTO_PTR_

KL_BEGIN
#ifdef _KL_USE_AUTO_PTR_
// CLASS auto_ptr {
template<typename Type>
class auto_ptr
{
private:
	KL_TYPE_DEFINE(Type);
	
	pointer _ptr;
	void _set(pointer &ptr)
	{
		if(_ptr) delete _ptr;
		_ptr = ptr;
	}
	void _get() const
	{
		return _ptr;
	}
public:
	explicit auto_ptr(pointer ptr = pointer(0)) : _ptr(ptr)
	{
	}
	auto_ptr(auto_ptr &o) 
		: _ptr(o._ptr)
	{
		o._ptr = 0;
	}
	template<typename Type2>
	auto_ptr(auto_ptr<Type2> &o)
		: _ptr(o._ptr)
	{
		o._ptr = 0;
	}
	auto_ptr &operator = (auto_ptr &o)
	{
		_set(o._ptr);
		o._ptr = 0;
	}
	template<typename Type2>
	auto_ptr &operator = (auto_ptr<Type2> &o)
	{
		this->_set(o._ptr);
		o._ptr = 0;
	}
	pointer get()
	{
		return _ptr;
	}
	pointer operator->() const
	{
		return _ptr;
	}
	reference operator * ()
	{
		return *_ptr;
	}
	const_reference operator * () const
	{
		return *_ptr;
	}
	pointer release()
	{
		pointer temp = _ptr;
		_ptr = 0;
		return temp;
	}
	void reset(pointer ptr)
	{
		this->_set(ptr);
	}
};
// CLASS auto_ptr }
#endif // _KL_USE_AUTO_PTR_
KL_END
